package com.isoft.c2team3service2.config;

import com.isoft.c2team3service2.mapper.PersonMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 蓝莓
 * @date 2025/6/22 10:02
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private PersonMapper personMapper;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        登录拦截器
        registry.addInterceptor(new LoginInterceptor(stringRedisTemplate))
                //, "/posts/**","/comments/**","/email/**","/file/**","/insert/**","/sensitive-words/**","/redis/**"
                //只有被拦截之后UserHolder才有数据,以下是不会被拦截的
                .excludePathPatterns("/login/**",
                        "/comments/article/{articleId}",
                        "/email/**",
                        "/sensitive-words/**",
                        "/posts/latest",
                        "/posts/hot",
                        "/redis/**",
                        "/insert/**")
                .order(1);
        //拦截所有请求，并且刷新token
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate,personMapper))
                .addPathPatterns("/**")
                .order(0);
    }
//@Override
//public void addInterceptors(InterceptorRegistry registry) {
//    // 1. 先配置 RefreshTokenInterceptor，确保所有请求都经过 Token 处理
//    registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate, personMapper))
//            .addPathPatterns("/**")
//            .order(0); // 最高优先级
//
//    // 2. 再配置 LoginInterceptor，只拦截需要登录的路径
//    registry.addInterceptor(new LoginInterceptor(stringRedisTemplate))
//            .addPathPatterns("/posts/**", "/comments/**", "/users/**") // 明确需要登录的路径
//            .excludePathPatterns("/login/**", "/register/**") // 排除不需要登录的路径
//            .order(1); // 次优先级
//}

}